package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.pojo.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    @GetMapping("/list")
    public R list(){
        Long userId = BaseContext.getId();
        LambdaQueryWrapper<ShoppingCart> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userId);


        List<ShoppingCart> list = shoppingCartService.list(wrapper);


        return R.success(list);

    }
    @PostMapping("/add")
    public R addShoppingCart(@RequestBody ShoppingCart shoppingCart, HttpSession session){
//        Long userId = (Long) session.getAttribute("userId");
        Long userId = BaseContext.getId();
        shoppingCart.setUserId(userId);
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
LambdaQueryWrapper<ShoppingCart> wrapper=new LambdaQueryWrapper<>();

        if (dishId!=null){

            wrapper.eq(ShoppingCart::getDishId,dishId);

        }else {
            wrapper.eq(ShoppingCart::getSetmealId,setmealId);
        }
        ShoppingCart one = shoppingCartService.getOne(wrapper);
        if (one==null){
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
            return R.success(shoppingCart);

        }else {

            one.setNumber(one.getNumber()+1);

            shoppingCartService.updateById(one);

        }

        return R.success(one);
    }

    @PostMapping("/sub")
    public R subShoppingCart(@RequestBody ShoppingCart shoppingCart){

        Long userId = BaseContext.getId();
        Long dishId = shoppingCart.getDishId();
        shoppingCart.setUserId(userId);
        Long setmealId = shoppingCart.getSetmealId();
        LambdaQueryWrapper<ShoppingCart> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userId);
        if (dishId!=null){

            wrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            wrapper.eq(ShoppingCart::getSetmealId,setmealId);
        }
        ShoppingCart one = shoppingCartService.getOne(wrapper);

        if (one!=null) {
            Integer number = one.getNumber();
            number--;


            if (number == 0) {
                one.setNumber(number);
                shoppingCartService.removeById(one);
                return R.success(one);
            }

            one.setNumber(number);
            shoppingCartService.updateById(one);
        }
        return R.success(one);



    }
    @DeleteMapping("/clean")
    public R clean(){

        Long userId = BaseContext.getId();
        LambdaQueryWrapper<ShoppingCart> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userId);
        shoppingCartService.remove(wrapper);
        return R.success("删除成功！");

    }



}
